package db;

import java.awt.Color;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Combo;

public class quanlycauhoi {

	protected Shell shell;
	private Table tablequanlycauhoi;
	private Text txtmacauhoi;
	private Text txtB;
	private Text txtnoidung;
	private Text txtC;
	private Text txtD;
	private Text txtA;
	private Text txtdung;
    private Button bntXem;
    private Text txttimtheonodung;
    private Combo combotimkiem;
    MessageBox ms;
    private Combo txtmalinhvuc;
    private Combo txtmucdo;
    private int thuchien=0;
    private Combo combotimtheolinhvuc ;
    private Combo combotimtheomucdo;
    private Button bnttim ;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			quanlycauhoi window = new quanlycauhoi();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
			//	display.sleep();
				if(thuchien==0)
				{
					loaddatacauhoi();
					thuchien=1;
					
					
				}
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(992, 514);
		shell.setText("Quan lý câu hỏi");
		shell.setLocation(100, 70);
		Composite menu = new Composite(shell, SWT.NONE);
	
		menu.setBounds(0, 10, 966, 91);
		
		 bntXem = new Button(menu, SWT.NONE);
		 bntXem.addSelectionListener(new SelectionAdapter() {
		 	@Override
		 	public void widgetSelected(SelectionEvent e) {
		 		
		 		loaddatacauhoi();
		 	}
		 });
		 bntXem.setForeground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		
	
		
		
	
		bntXem.setBounds(50, 0, 75, 64);
		bntXem.setText("");
		
		Button bntxoa = new Button(menu, SWT.NONE);
		bntxoa.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(txtmacauhoi.isEnabled())
				
				txtmacauhoi.setEnabled(false);
				if(!txtmacauhoi.getText().equals(""))
				{
					cauhoicoso ch=new cauhoicoso();
					ch.Delete(txtmacauhoi.getText());
				loaddatacauhoi();
				txtmacauhoi.setText("");
				txtnoidung.setText("");
				txtmalinhvuc.setText("");
				txtmucdo.setText("");
				txtA.setText("");
				txtB.setText("");
				txtC.setText("");
				txtD.setText("");
				txtdung.setText("");
				
					
				}
				else{
					ms=new MessageBox(shell);
					ms.setMessage("bạn chưa chọn thông tin cần xóa \n vui lòng chọn lại!");
					ms.open();
					
				}
				
			}
		});
		bntxoa.setBounds(162, 0, 75, 64);
		
		Button bntsua = new Button(menu, SWT.NONE);
		bntsua.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(txtmacauhoi.isEnabled())
				txtmacauhoi.setEnabled(false);
				if(!txtmacauhoi.getText().equals(""))
				{
					try{
						ms=new MessageBox(shell,SWT.ICON_QUESTION |SWT.YES|SWT.NO);
						ms.setMessage("bạn thực sự muốn xóa dữ liệu này!");
						int str=ms.open();
						if(str==64)
						{
						cauhoicoso chcs=new cauhoicoso(null, txtmacauhoi.getText(), txtnoidung.getText(),txtmalinhvuc.getText(), txtmucdo.getText(), txtA.getText(), txtB.getText(),txtC.getText(), txtD.getText(),txtdung.getText());
						chcs.Update();
						loaddatacauhoi();
						}
						
					}catch(Exception e1)
					{
						ms.setMessage("bạn chưa chon mã lĩnh vực \n vui lọng chọn lại!");
						ms.open();
					}
					
				}
				else
				{
					System.out.print("%c"+7);
					ms=new MessageBox(shell);;
					ms.setMessage("bạn chưa chọn thông tin cản sữa \n  vui lòng nhập lại");
					ms.open();
					
				}
			}
		});
		bntsua.setBounds(285, 0, 75, 64);
		
		Button bntinsert = new Button(menu, SWT.NONE);
		bntinsert.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(!txtmacauhoi.isEnabled())
					txtmacauhoi.setEnabled(true);
		
				if(txtmacauhoi.getText().equals("")|| txtmalinhvuc.getText().equals(""))
				{
					
					ms=new MessageBox(shell);
					ms.setMessage("bạn chưa nhập thông tin cần insert vào trong database! \n vui lòng nhập lại thông tin");
					ms.open();
					
				}else
				{
					try{
						ms=new MessageBox(shell,SWT.ICON_QUESTION |SWT.YES|SWT.NO);
						ms.setMessage("bạn thực sự muốn them vào dữ liệu này!");
						int str=ms.open();
						if(str==64)
						{
						cauhoicoso ch=new cauhoicoso(null, txtmacauhoi.getText(), txtnoidung.getText(), txtmalinhvuc.getText(), txtmucdo.getText(), txtA.getText(), txtB.getText(), txtC.getText(), txtD.getText(), txtdung.getText());
						ch.Save();
						loaddatacauhoi();
						}
							
					   
					}catch(Exception e1)
					{
						
						
						
					}
					
				}
			
			}
		});
		bntinsert.setBounds(408, 0, 75, 64);
		
		Label lblNewLabel = new Label(menu, SWT.NONE);
		lblNewLabel.setAlignment(SWT.CENTER);
		lblNewLabel.setBounds(50, 70, 75, 21);
		lblNewLabel.setText("XEM");
		
		Label lblXa = new Label(menu, SWT.NONE);
		lblXa.setText("XÓA");
		lblXa.setAlignment(SWT.CENTER);
		lblXa.setBounds(162, 70, 75, 21);
		
		Label lblSa = new Label(menu, SWT.NONE);
		lblSa.setText("SỬA");
		lblSa.setAlignment(SWT.CENTER);
		lblSa.setBounds(285, 70, 75, 21);
		
		Label lblCpNht = new Label(menu, SWT.NONE);
		lblCpNht.setText("THÊM");
		lblCpNht.setAlignment(SWT.CENTER);
		lblCpNht.setBounds(408, 70, 75, 21);
		
		Group grouptimiem = new Group(menu, SWT.NONE);
		grouptimiem.setBounds(620, 9, 336, 82);
		grouptimiem.setText("Phần Tìm Kiếm");
		
		txttimtheonodung = new Text(grouptimiem, SWT.BORDER);
		txttimtheonodung.setBounds(177, 20, 110, 21);
		txttimtheonodung.setVisible(false);
		
		 combotimkiem = new Combo(grouptimiem, SWT.NONE);
		combotimkiem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(combotimkiem.getSelectionIndex()==2)
				{
					bnttim.setVisible(false);
					Timtheomacauhoi ttmch=new Timtheomacauhoi();
					ttmch.open();
					
					
					
					
				}
				
				else if(combotimkiem.getSelectionIndex()==0)
				{
					bnttim.setVisible(false);
				if(!combotimtheomucdo.isVisible())	
				{
					combotimtheomucdo.setVisible(true);
					
				}
				if(combotimtheolinhvuc.isVisible())
					combotimtheolinhvuc.setVisible(false);
				if(txttimtheonodung.isVisible())
					txttimtheonodung.setVisible(false);
				}
				else if(combotimkiem.getSelectionIndex()==1)
				{
					bnttim.setVisible(false);
					if(!combotimtheolinhvuc.isVisible())
						combotimtheolinhvuc.setVisible(true);
					if(combotimtheomucdo.isVisible())
						combotimtheomucdo.setVisible(false);
					if(txttimtheonodung.isVisible())
						txttimtheonodung.setVisible(false);
					
					
				}
				else if(combotimkiem.getSelectionIndex()==3)
				{
					if(!txttimtheonodung.isVisible())
						txttimtheonodung.setVisible(true);
					if(combotimtheolinhvuc.isVisible())
						combotimtheolinhvuc.setVisible(false);
					if(combotimtheomucdo.isVisible())
						combotimtheomucdo.setVisible(false);
					if(!bnttim.isVisible())
						bnttim.setVisible(true);
					
				}
				
			}
		});
		combotimkiem.setItems(new String[] {"Mức Độ", "Lĩnh Vực", "Theo Thứ Tự","Nội Dung Câu Hỏi"});
		combotimkiem.setBounds(10, 20, 128, 23);
		
		combotimtheomucdo = new Combo(grouptimiem, SWT.NONE);
		combotimtheomucdo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int i=combotimtheomucdo.getSelectionIndex();
				
				loaddatagon(i);
				
			}
		});
		combotimtheomucdo.setItems(new String[] {"trung bình", "dễ", "khó"});
		combotimtheomucdo.setBounds(187, 20, 110, 23);
		combotimtheomucdo.setVisible(false);
		 combotimtheolinhvuc = new Combo(grouptimiem, SWT.NONE);
		 combotimtheolinhvuc.addSelectionListener(new SelectionAdapter() {
		 	@Override
		 	public void widgetSelected(SelectionEvent e) {
		 	int i=combotimtheolinhvuc.getSelectionIndex();
		 	loaddataLV(i);
		 	}
		 });
		 combotimtheolinhvuc.setItems(new String[] {"du lịch", "khoa học", "lịch sữ", "sinh học", "toán", "tự nhiên", "vật lý", "xã hội"});
		combotimtheolinhvuc.setBounds(187, 20, 110, 23);
		combotimtheolinhvuc.setVisible(false);
		
		 bnttim = new Button(grouptimiem, SWT.NONE);
		 bnttim.addSelectionListener(new SelectionAdapter() {
		 	@Override
		 	public void widgetSelected(SelectionEvent e) {
		 		
		 		selectnoidung(txttimtheonodung.getText());
		 	}
		 });
		bnttim.setBounds(155, 47, 75, 25);
		bnttim.setText("Tìm");
		
		Button bntthemlinhvuc = new Button(menu, SWT.NONE);
		bntthemlinhvuc.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		bntthemlinhvuc.setBounds(525, 0, 75, 64);
		
		Label lblThmLnhVc = new Label(menu, SWT.NONE);
		lblThmLnhVc.setText("THÊM LĨNH VỰC");
		lblThmLnhVc.setAlignment(SWT.CENTER);
		lblThmLnhVc.setBounds(511, 70, 103, 21);
		
		Composite combo = new Composite(shell, SWT.NONE);
		combo.setBounds(0, 104, 966, 362);
		
		tablequanlycauhoi = new Table(combo, SWT.BORDER | SWT.FULL_SELECTION);
		tablequanlycauhoi.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int i=  tablequanlycauhoi.getSelectionIndex();
				txtmacauhoi.setText(tablequanlycauhoi.getItem(i).getText(1));
				txtnoidung.setText(tablequanlycauhoi.getItem(i).getText(2));
				txtmalinhvuc.setText(tablequanlycauhoi.getItem(i).getText(3));
				txtmucdo.setText(tablequanlycauhoi.getItem(i).getText(4));
				
				txtA.setText(tablequanlycauhoi.getItem(i).getText(5));
				txtB.setText(tablequanlycauhoi.getItem(i).getText(6));
				txtC.setText(tablequanlycauhoi.getItem(i).getText(7));
				txtD.setText(tablequanlycauhoi.getItem(i).getText(8));
				txtdung.setText(tablequanlycauhoi.getItem(i).getText(9));
			}
		});
		tablequanlycauhoi.setBounds(0, 117, 956, 235);
		tablequanlycauhoi.setHeaderVisible(true);
		tablequanlycauhoi.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn.setWidth(30);
		tblclmnNewColumn.setText("id");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_1.setWidth(80);
		tblclmnNewColumn_1.setText("mã câu hỏi");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_2.setWidth(231);
		tblclmnNewColumn_2.setText("nội dung");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_3.setWidth(82);
		tblclmnNewColumn_3.setText("mã lĩnh vực");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_4.setWidth(75);
		tblclmnNewColumn_4.setText("mức độ");
		
		TableColumn tblclmnNewColumn_5 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_5.setWidth(100);
		tblclmnNewColumn_5.setText("A");
		
		TableColumn tblclmnNewColumn_6 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_6.setWidth(100);
		tblclmnNewColumn_6.setText("B");
		
		TableColumn tblclmnNewColumn_7 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_7.setWidth(100);
		tblclmnNewColumn_7.setText("C");
		
		TableColumn tblclmnNewColumn_8 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_8.setWidth(100);
		tblclmnNewColumn_8.setText("D");
		
		TableColumn tblclmnNewColumn_9 = new TableColumn(tablequanlycauhoi, SWT.CENTER);
		tblclmnNewColumn_9.setWidth(52);
		tblclmnNewColumn_9.setText("đúng");
		
		txtmacauhoi = new Text(combo, SWT.BORDER);
		txtmacauhoi.setBounds(26, 25, 87, 21);
		
		
		txtB = new Text(combo, SWT.BORDER);
		txtB.setBounds(26, 78, 102, 21);
		
		txtnoidung = new Text(combo, SWT.BORDER);
		txtnoidung.setBounds(126, 25, 422, 21);
		
		txtC = new Text(combo, SWT.BORDER);
		txtC.setBounds(162, 78, 102, 21);
		
		txtD = new Text(combo, SWT.BORDER);
		txtD.setBounds(290, 78, 102, 21);
		
		txtA = new Text(combo, SWT.BORDER);
		txtA.setBounds(788, 25, 126, 21);
		
		txtdung = new Text(combo, SWT.BORDER);
		txtdung.setBounds(434, 78, 113, 21);
		
		Label lblNewLabel_1 = new Label(combo, SWT.CENTER);
		lblNewLabel_1.setBounds(26, 4, 87, 15);
		lblNewLabel_1.setText("mã câu hỏi");
		
		Label lblNewLabel_2 = new Label(combo, SWT.CENTER);
		lblNewLabel_2.setBounds(126, 4, 422, 15);
		lblNewLabel_2.setText("Nội Dung");
		
		Label lblNewLabel_3 = new Label(combo, SWT.CENTER);
		lblNewLabel_3.setBounds(564, 4, 95, 15);
		lblNewLabel_3.setText("Mã Lĩnh Vực");
		
		Label lblNewLabel_4 = new Label(combo, SWT.CENTER);
		lblNewLabel_4.setBounds(680, 4, 90, 15);
		lblNewLabel_4.setText("Mức Độ");
		
		Label lblNewLabel_5 = new Label(combo, SWT.CENTER);
		lblNewLabel_5.setBounds(792, 4, 122, 15);
		lblNewLabel_5.setText("A");
		
		Label lblNewLabel_6 = new Label(combo, SWT.CENTER);
		lblNewLabel_6.setBounds(26, 57, 102, 15);
		lblNewLabel_6.setText("B");
		
		Label label = new Label(combo, SWT.CENTER);
		label.setText("C");
		label.setBounds(162, 57, 102, 15);
		
		Label label_1 = new Label(combo, SWT.CENTER);
		label_1.setText("D");
		label_1.setBounds(290, 57, 102, 15);
		
		Label label_2 = new Label(combo, SWT.CENTER);
		label_2.setText("Đúng");
		label_2.setBounds(434, 57, 114, 15);
		
		 txtmalinhvuc = new Combo(combo, SWT.NONE);
		txtmalinhvuc.setItems(new String[] {"XH", "TN", "VL", "KH", "LS", "SH"});
		txtmalinhvuc.setBounds(564, 25, 91, 23);
		
		 txtmucdo = new Combo(combo, SWT.NONE);
		 txtmucdo.setItems(new String[] {"trung binh", "de", " kho"});
		txtmucdo.setBounds(679, 25, 91, 23);

	}
	public void loaddatacauhoi()
	{
		tablequanlycauhoi.removeAll();
		cauhoicoso chcs=new cauhoicoso();
				 
		try {
			ResultSet rs= chcs.SelectAll();
			while(rs.next())
			{
				TableItem item=new TableItem(tablequanlycauhoi, SWT.None);
				item.setText(new String []{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10)});
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	public void loaddatagon(int i)
	{
		tablequanlycauhoi.removeAll();
		String str="";
		if(i==0)
		{
			str="trung binh";
			
		}
		
		else if(i==1)
			str="de";
		else if(i==2)
			str="kho";
		cauhoicoso ch=new cauhoicoso();
		try {
			ResultSet rs=ch.Selectone(str);
			while(rs.next())
			{
				TableItem item=new TableItem(tablequanlycauhoi, SWT.None);
				
				item.setText(new String []{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10)});
			}
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	
	public void loaddataLV(int i)
	{
		
		
		tablequanlycauhoi.removeAll();
		
		String str="";
		if(i==0)
		{
			str="DL";
			
		}
		else if(i==1)
		{
		 str="KH";	
		}
		else if(i==2)
		{
			str="LS";
		}
		else if(i==3)
		{
			str="SH";
		}
		else if(i==4)
		{
			str="T";
		}
		else if(i==5)
		{
			str="TN";;
		}
		else if(i==6)
		{
			str="VL";
		}
		else if(i==7)
		{
			str="XH";
		}
		cauhoicoso ch=new cauhoicoso();
		try {
			ResultSet rs=ch.SelectoneLV(str);
			while(rs.next())
			{
				TableItem item=new TableItem(tablequanlycauhoi, SWT.None);
				item.setText(new String []{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10)});
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void selectnoidung(String str)
	{
		tablequanlycauhoi.removeAll();
		cauhoicoso ch=new cauhoicoso();
		try {
			ResultSet rs=ch.SelectoneND(str);
			while(rs.next())
			{
				TableItem item=new TableItem(tablequanlycauhoi, SWT.None);
				item.setText(new String []{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10)});
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
